<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1, user-scalable=no">
  <meta name="description" content="API docs for the timeout method from the Stream class, for the Dart programming language.">
  <title>timeout method - Stream class - dart:async library - Dart API</title>
  <!-- required because all the links are pseudo-absolute -->
  <base href="../..">

  <link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500,400i,400,300|Source+Sans+Pro:400,300,700" rel="stylesheet">
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  <link rel="stylesheet" href="static-assets/github.css">
  <link rel="stylesheet" href="static-assets/styles.css">
  <link rel="icon" href="static-assets/favicon.png">
  
</head>

<body>

<div id="overlay-under-drawer"></div>

<header id="title">
  <button id="sidenav-left-toggle" type="button">&nbsp;</button>
  <ol class="breadcrumbs gt-separated dark hidden-xs">
    <li><a href="index.html">shake</a></li>
    <li><a href="dart-async/dart-async-library.html">dart:async</a></li>
    <li><a href="dart-async/Stream-class.html">Stream<span class="signature">&lt;<wbr><span class="type-parameter">T</span>&gt;</span></a></li>
    <li class="self-crumb">timeout method</li>
  </ol>
  <div class="self-name">timeout</div>
  <form class="search navbar-right" role="search">
    <input type="text" id="search-box" autocomplete="off" disabled class="form-control typeahead" placeholder="Loading search...">
  </form>
</header>

<main>

  <div id="dartdoc-sidebar-left" class="col-xs-6 col-sm-3 col-md-2 sidebar sidebar-offcanvas-left">
    <header id="header-search-sidebar" class="hidden-l">
      <form class="search-sidebar" role="search">
        <input type="text" id="search-sidebar" autocomplete="off" disabled class="form-control typeahead" placeholder="Loading search...">
      </form>
    </header>
    
    <ol class="breadcrumbs gt-separated dark hidden-l" id="sidebar-nav">
      <li><a href="index.html">shake</a></li>
      <li><a href="dart-async/dart-async-library.html">dart:async</a></li>
      <li><a href="dart-async/Stream-class.html">Stream<span class="signature">&lt;<wbr><span class="type-parameter">T</span>&gt;</span></a></li>
      <li class="self-crumb">timeout method</li>
    </ol>
    
    <h5>Stream class</h5>
    <ol>
      <li class="section-title"><a href="dart-async/Stream-class.html#constructors">Constructors</a></li>
      <li><a href="dart-async/Stream/Stream.html">Stream</a></li>
      <li><a href="dart-async/Stream/Stream.empty.html">empty</a></li>
      <li><a href="dart-async/Stream/Stream.eventTransformed.html">eventTransformed</a></li>
      <li><a href="dart-async/Stream/Stream.fromFuture.html">fromFuture</a></li>
      <li><a href="dart-async/Stream/Stream.fromFutures.html">fromFutures</a></li>
      <li><a href="dart-async/Stream/Stream.fromIterable.html">fromIterable</a></li>
      <li><a href="dart-async/Stream/Stream.periodic.html">periodic</a></li>
    
      <li class="section-title">
        <a href="dart-async/Stream-class.html#instance-properties">Properties</a>
      </li>
      <li><a href="dart-async/Stream/first.html">first</a></li>
      <li><a href="dart-async/Stream/isBroadcast.html">isBroadcast</a></li>
      <li><a href="dart-async/Stream/isEmpty.html">isEmpty</a></li>
      <li><a href="dart-async/Stream/last.html">last</a></li>
      <li><a href="dart-async/Stream/length.html">length</a></li>
      <li><a href="dart-async/Stream/single.html">single</a></li>
      <li class="inherited"><a href="dart-core/Object/hashCode.html">hashCode</a></li>
      <li class="inherited"><a href="dart-core/Object/runtimeType.html">runtimeType</a></li>
    
      <li class="section-title"><a href="dart-async/Stream-class.html#instance-methods">Methods</a></li>
      <li><a href="dart-async/Stream/any.html">any</a></li>
      <li><a href="dart-async/Stream/asBroadcastStream.html">asBroadcastStream</a></li>
      <li><a href="dart-async/Stream/asyncExpand.html">asyncExpand</a></li>
      <li><a href="dart-async/Stream/asyncMap.html">asyncMap</a></li>
      <li><a href="dart-async/Stream/cast.html">cast</a></li>
      <li><a href="dart-async/Stream/contains.html">contains</a></li>
      <li><a href="dart-async/Stream/distinct.html">distinct</a></li>
      <li><a href="dart-async/Stream/drain.html">drain</a></li>
      <li><a href="dart-async/Stream/elementAt.html">elementAt</a></li>
      <li><a href="dart-async/Stream/every.html">every</a></li>
      <li><a href="dart-async/Stream/expand.html">expand</a></li>
      <li><a href="dart-async/Stream/firstWhere.html">firstWhere</a></li>
      <li><a href="dart-async/Stream/fold.html">fold</a></li>
      <li><a href="dart-async/Stream/forEach.html">forEach</a></li>
      <li><a href="dart-async/Stream/handleError.html">handleError</a></li>
      <li><a href="dart-async/Stream/join.html">join</a></li>
      <li><a href="dart-async/Stream/lastWhere.html">lastWhere</a></li>
      <li><a href="dart-async/Stream/listen.html">listen</a></li>
      <li><a href="dart-async/Stream/map.html">map</a></li>
      <li><a href="dart-async/Stream/pipe.html">pipe</a></li>
      <li><a href="dart-async/Stream/reduce.html">reduce</a></li>
      <li><a href="dart-async/Stream/singleWhere.html">singleWhere</a></li>
      <li><a href="dart-async/Stream/skip.html">skip</a></li>
      <li><a href="dart-async/Stream/skipWhile.html">skipWhile</a></li>
      <li><a href="dart-async/Stream/take.html">take</a></li>
      <li><a href="dart-async/Stream/takeWhile.html">takeWhile</a></li>
      <li><a href="dart-async/Stream/timeout.html">timeout</a></li>
      <li><a href="dart-async/Stream/toList.html">toList</a></li>
      <li><a href="dart-async/Stream/toSet.html">toSet</a></li>
      <li><a href="dart-async/Stream/transform.html">transform</a></li>
      <li><a href="dart-async/Stream/where.html">where</a></li>
      <li class="inherited"><a href="dart-core/Object/noSuchMethod.html">noSuchMethod</a></li>
      <li class="inherited"><a href="dart-core/Object/toString.html">toString</a></li>
    
      <li class="section-title inherited"><a href="dart-async/Stream-class.html#operators">Operators</a></li>
      <li class="inherited"><a href="dart-core/Object/operator_equals.html">operator ==</a></li>
    
    
      <li class="section-title"><a href="dart-async/Stream-class.html#static-methods">Static methods</a></li>
      <li><a href="dart-async/Stream/castFrom.html">castFrom</a></li>
    
    </ol>
  </div><!--/.sidebar-offcanvas-->

  <div id="dartdoc-main-content" class="col-xs-12 col-sm-9 col-md-8 main-content">
      <div><h1>timeout method</h1></div>

    <section class="multi-line-signature">
      <span class="returntype"><a href="dart-async/Stream-class.html">Stream</a><span class="signature">&lt;<wbr><span class="type-parameter">T</span>&gt;</span></span>
            <span class="name ">timeout</span>
(<wbr><span class="parameter" id="timeout-param-timeLimit"><span class="type-annotation"><a href="dart-core/Duration-class.html">Duration</a></span> <span class="parameter-name">timeLimit</span>, {</span> <span class="parameter" id="timeout-param-onTimeout"><span class="type-annotation">void</span> <span class="parameter-name">onTimeout</span>(<span class="parameter" id="onTimeout-param-sink"><span class="type-annotation"><a href="dart-async/EventSink-class.html">EventSink</a><span class="signature">&lt;<wbr><span class="type-parameter">T</span>&gt;</span></span> <span class="parameter-name">sink</span></span>)</span> })
      
    </section>
    <section class="desc markdown">
      <p>Creates a new stream with the same events as this stream.</p>
<p>Whenever more than <code>timeLimit</code> passes between two events from this stream,
the <code>onTimeout</code> function is called, which can emit further events on
the returned stream.</p>
<p>The countdown doesn't start until the returned stream is listened to.
The countdown is reset every time an event is forwarded from this stream,
or when this stream is paused and resumed.</p>
<p>The <code>onTimeout</code> function is called with one argument: an
<a href="dart-async/EventSink-class.html">EventSink</a> that allows putting events into the returned stream.
This <code>EventSink</code> is only valid during the call to <code>onTimeout</code>.
Calling <a href="dart-async/EventSink/close.html">EventSink.close</a> on the sink passed to <code>onTimeout</code> closes the
returned stream, and no further events are processed.</p>
<p>If <code>onTimeout</code> is omitted, a timeout will just put a <a href="dart-async/TimeoutException-class.html">TimeoutException</a>
into the error channel of the returned stream.
If the call to <code>onTimeout</code> throws, the error is emitted on the returned
stream.</p>
<p>The returned stream is a broadcast stream if this stream is.
If a broadcast stream is listened to more than once, each subscription
will have its individually timer that starts counting on listen,
and the subscriptions' timers can be paused individually.</p>
    </section>
    
    <section class="summary source-code" id="source">
      <h2><span>Implementation</span></h2>
      <pre class="language-dart"><code class="language-dart">Stream&lt;T&gt; timeout(Duration timeLimit, {void onTimeout(EventSink&lt;T&gt; sink)}) {
  _StreamControllerBase&lt;T&gt; controller;
  &#47;&#47; The following variables are set on listen.
  StreamSubscription&lt;T&gt; subscription;
  Timer timer;
  Zone zone;
  _TimerCallback timeout;

  void onData(T event) {
    timer.cancel();
    controller.add(event);
    timer = zone.createTimer(timeLimit, timeout);
  }

  void onError(error, StackTrace stackTrace) {
    timer.cancel();
    assert(controller is _StreamController ||
        controller is _BroadcastStreamController);
    controller._addError(error, stackTrace); &#47;&#47; Avoid Zone error replacement.
    timer = zone.createTimer(timeLimit, timeout);
  }

  void onDone() {
    timer.cancel();
    controller.close();
  }

  void onListen() {
    &#47;&#47; This is the onListen callback for of controller.
    &#47;&#47; It runs in the same zone that the subscription was created in.
    &#47;&#47; Use that zone for creating timers and running the onTimeout
    &#47;&#47; callback.
    zone = Zone.current;
    if (onTimeout == null) {
      timeout = () {
        controller.addError(
            new TimeoutException(&quot;No stream event&quot;, timeLimit), null);
      };
    } else {
      &#47;&#47; TODO(floitsch): the return type should be &#39;void&#39;, and the type
      &#47;&#47; should be inferred.
      var registeredOnTimeout =
          zone.registerUnaryCallback&lt;dynamic, EventSink&lt;T&gt;&gt;(onTimeout);
      var wrapper = new _ControllerEventSinkWrapper&lt;T&gt;(null);
      timeout = () {
        wrapper._sink = controller; &#47;&#47; Only valid during call.
        zone.runUnaryGuarded(registeredOnTimeout, wrapper);
        wrapper._sink = null;
      };
    }

    subscription = this.listen(onData, onError: onError, onDone: onDone);
    timer = zone.createTimer(timeLimit, timeout);
  }

  Future onCancel() {
    timer.cancel();
    Future result = subscription.cancel();
    subscription = null;
    return result;
  }

  controller = isBroadcast
      ? new _SyncBroadcastStreamController&lt;T&gt;(onListen, onCancel)
      : new _SyncStreamController&lt;T&gt;(onListen, () {
          &#47;&#47; Don&#39;t null the timer, onCancel may call cancel again.
          timer.cancel();
          subscription.pause();
        }, () {
          subscription.resume();
          timer = zone.createTimer(timeLimit, timeout);
        }, onCancel);
  return controller.stream;
}</code></pre>
    </section>

  </div> <!-- /.main-content -->

  <div id="dartdoc-sidebar-right" class="col-xs-6 col-sm-6 col-md-2 sidebar sidebar-offcanvas-right">
  </div><!--/.sidebar-offcanvas-->

</main>

<footer>
  <span class="no-break">
    shake 0.1.0
  </span>

  
</footer>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="static-assets/typeahead.bundle.min.js"></script>
<script src="static-assets/highlight.pack.js"></script>
<script src="static-assets/URI.js"></script>
<script src="static-assets/script.js"></script>


</body>

</html>
